package com.wh.wisdomsite.xbox.ba.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.SqlHelper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.wh.wisdomsite.xbox.ba.dao.MenuRoleMapper;
import com.wh.wisdomsite.xbox.ba.dao.RoleMapper;
import com.wh.wisdomsite.xbox.ba.entity.MenuRole;
import com.wh.wisdomsite.xbox.ba.entity.Role;
import com.wh.wisdomsite.xbox.ba.service.IRoleService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * 角色ServiceImpl
 *
 * @version V1.0
 * @Package: com.wh.wisdomsite.xbox.us.service.impl
 * @author: 丁奕
 * @date: 2018-08-03 11:42
 * @Copyright 公司版权所有
 * @Company 贵州万华科技有限公司Copyright (c) 2016
 **/
@Service("roleService")
@Transactional(rollbackFor = {Exception.class,RuntimeException.class})
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

    private static final Log logger = LogFactory.getLog(RoleServiceImpl.class);

    @Autowired
    RoleMapper roleMapper;

    @Autowired
    MenuRoleMapper menuRoleMapper;

    @Override
    public Map<String, Object> getMapById(Serializable id) {
        return roleMapper.getMapById(id);
    }

    @Override
    public Page<Map<String, Object>> getMapsPage(Page page, Wrapper<Role> wrapper) {
        SqlHelper.fillWrapper(page, wrapper);
        page.setRecords(this.baseMapper.getMapsPage(page, wrapper));
        return page;
    }

    @Override
    public boolean deleteById(Serializable id) {
        //先删除关联的菜单中间表内容
        deleteMenuRoleByRoleId(id);
        return super.deleteById(id);
    }

    @Override
    public List<Map<String, Object>> selectListByCompanyId(Integer companyId) {
        return roleMapper.selectListByCompanyId(companyId);
    }

    @Override
    public List<Role> getRoleListByUserId(String id) {
        return roleMapper.getRoleListByUserId(id);
    }

    @Override
    public List<Role> getRoleListByNameAndCompId(String roleName, String compId,Integer parentCompId, String roId) {
        return roleMapper.getRoleListByNameAndCompId(roleName,compId,parentCompId,roId);
    }

    /**
     * 根据角色ID先删除所有关联的角色菜单
     * @param roId
     * @return
     */
    @Override
    public Integer deleteMenuRoleByRoleId(Serializable roId){
        EntityWrapper<MenuRole> ew = new EntityWrapper<>();
        ew.setEntity(new MenuRole());
        ew.eq("roId",roId);
        return menuRoleMapper.delete(ew);
    }
}
